package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.pojo.Emp;
import com.woniu.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class EmpController {
    @Autowired
    EmpService empService;
    //增加一个员工
    @RequestMapping("empSave")
    public String empSave(Emp emp){
        empService.save(emp);
        return "redirect:empList";
    }
    //删除一个员工
    @RequestMapping("delEmp")
    public String delEmp(Integer empno){
        empService.delEmp(empno);
        return "redirect:empList";
    }
    //找到一个员工
    @RequestMapping("getOneEmp")
    public String getOneEmp(Integer empno, Model model){
        System.out.println(empno);
        System.out.println(111+"-------------");
        System.out.println(empService.getOneEmp(empno));
       model.addAttribute(empService.getOneEmp(empno));
       return "empUpdate1";
    }
    //修改保存员工
    @RequestMapping("updateEmp")
    public String updateEmp(Emp emp){
        empService.updateEmp(emp);
        return "redirect:empList";
    }
    //查询原员工
    @RequestMapping("empList")
    public String empSelect(Emp emp, Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Emp> empList=empService.empSelect(emp);
        PageInfo<Emp> pageInfo=new PageInfo<Emp>(empList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("empList",empList);
        return "empList";
    }
    //批量删除
    @RequestMapping("delEmpSelect")
    public String delEmpSelect(Integer[] empnos){
        empService.delSelect(empnos);
        return "redirect:empList";
    }
}
